# coding=utf-8
from __future__ import unicode_literals

from django.db import models


# Create your models here.
class Project(models.Model):
    name = models.CharField(max_length=20, verbose_name=u'项目', blank=False)
    create_time = models.DateField(verbose_name=u'添加时间', auto_now=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'项目'
        verbose_name_plural = u'项目'
        ordering = ['-id']


class Module(models.Model):
    project_id = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name=u'项目')
    name = models.CharField(max_length=50, verbose_name=u'模块', unique=True)
    person_in_charge = models.CharField(max_length=20, verbose_name=u'负责人', blank=True)
    completion_rate = models.DecimalField(max_digits=4, decimal_places=1, verbose_name=u'完成率(%)', default=0)

    def __unicode__(self):
        return u'%s--%s' % (self.project_id, self.name)

    class Meta:
        verbose_name = u'模块'
        verbose_name_plural = u'模块'
        ordering = ['-id']


class Function(models.Model):
    YESNO_CHOICES = [(False, u'未完成'), (True, u'完成')]
    project_id = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name=u'项目', related_name='project')
    module_id = models.ForeignKey(Module, on_delete=models.CASCADE, verbose_name=u'模块')
    function_point = models.TextField(max_length=200, verbose_name=u"功能点")
    completion = models.BooleanField(default=False, verbose_name=u'是否完成', choices=YESNO_CHOICES)

    class Meta:
        verbose_name = u'功能点'
        verbose_name_plural = u'功能点'


class AccessLog(models.Model):
    route = models.CharField(max_length=200, null=True, editable=False)
    ip = models.GenericIPAddressField(null=False, editable=False)
    access_time = models.DateField(verbose_name=u'访问时间', auto_now=True)

    class Meta:
        verbose_name = u'访问记录'
        verbose_name_plural = u'访问记录'
